package com.itic.web.mem.order.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itic.appbase.applications.sys.common.service.OutCommonService;
import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.utils.RandomOrderNumHelper;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.ht.mem.member.persistence.model.WebMember;
import com.itic.ht.mem.member.service.WebMemberService;
import com.itic.ht.mem.mtype.persistence.model.WebmType;
import com.itic.ht.mem.mtype.service.WebmTypeService;
import com.itic.ht.mem.order.persistence.model.MemberOrder;
import com.itic.ht.mem.order.persistence.model.MemberOrderExample;
import com.itic.ht.mem.order.service.MemberOrderService;
import com.itic.ht.mem.orderdetail.persistence.model.MemberOrderDetail;
import com.itic.ht.mem.orderdetail.service.MemberOrderDetailService;
import com.itic.web.mem.order.vo.MemberOrderVo;
import com.itic.web.qo.TokenData;
import com.itic.web.ro.ResResult;
import com.itic.web.util.TokenUtil;


@Service
@Transactional
public class AppOrderService {
    @Autowired
    private MemberOrderService memberOrderService;
    @Autowired
    private MemberOrderDetailService memberOrderDetailService;
	@Autowired
	private WebMemberService webMemberService;
	@Autowired
	private WebmTypeService webmTypeService;
    @Autowired
	protected OutCommonService outCommonService;
    
    
    public ResResult getOrder(MemberOrder temp,TokenData data,String itemId){
		try {
			//判断token是不是存在
			if(StringHelper.isEmpty(data.getToken())){
				return ResResult.error(1, "token不能为空");
			}
			//先判断token正确性
			boolean flag = TokenUtil.assertUserToken(data);
			if(!flag){
				return ResResult.error(2, "token失效");
			}
			//判断订单类型Id是不是存在
			if(StringHelper.isEmpty(itemId)){
				return ResResult.error(1, "itemId不能为空");
			}
			
			//从token中取account信息   
			data.setAccount(TokenUtil.getUserList(data.getToken()).getAccount().trim());
			//取得用户信息
			WebMember member = webMemberService.getMemberByAccount(data.getAccount());
			System.out.println("现在要查询的帐号是:"+member.getAccount());
			
			//添加支付订单           itemId  -----
/*		    private String account;----
		    private Long orderNum;
		    private BigDecimal totalPrice;
		    private Integer itemCount;
		    private Byte status;
		    private Byte source;
		    */
			//判断用户支付定金还是全额支付
			if(StringHelper.isBlank(temp.getOrderType())){
				return ResResult.error(400, "支付信息不全");
			}
			
			//生成订单号
			String orderNum = RandomOrderNumHelper.getOrderNumber();
			temp.setOrderNum(orderNum);
			temp.setItemCount(1);
			BigDecimal mTypePrice = null;
			if(DBConst.PAY.equals(temp.getOrderType())){
				mTypePrice = new BigDecimal(webmTypeService.get(itemId).getPrice().toString());
			}else if(DBConst.DEPOSIT.equals(temp.getOrderType())){
				mTypePrice = new BigDecimal(webmTypeService.get(itemId).getDepositPrice().toString());
			}else {
				return ResResult.error(4001, "支付类型有误");
			}
			temp.setTotalPrice(mTypePrice);
			
			temp.setStatus("0");
			
/*		    private Long orderNum;
		    private Integer itemId;
		    private Integer count;
		    private BigDecimal totalPrice;*/
			
			//添加商品订单
			MemberOrderDetail detail = new MemberOrderDetail();
			detail.setOrderNum(orderNum);
			detail.setItemId(itemId);
			detail.setCount(1);
			detail.setTotalPrice(mTypePrice);
			detail.setStatus("0");
			memberOrderDetailService.add(detail);
			
			//添加支付订单
			temp.setAccount(member.getAccount());
			memberOrderService.add(temp);
			
			MemberOrderVo vo = new MemberOrderVo();
			MemberOrder backOrder = memberOrderService.getMemberOrderByOrderNum(orderNum);
			vo.setId(backOrder.getId());
			vo.setAccount(backOrder.getAccount());
			vo.setOrderNum(backOrder.getOrderNum());
			vo.setRealname(member.getRealname());
			vo.setItemCount(backOrder.getItemCount());
			vo.setTotalPrice(backOrder.getTotalPrice());
			vo.setNote(backOrder.getNote());
			vo.setSource(backOrder.getSource());
			vo.setStatus(backOrder.getStatus());
			vo.setItemId(itemId);
			WebmType webmType = webmTypeService.get(itemId);
			vo.setItemName(webmType.getName());
			vo.setItemType(webmType.getCategory());
			vo.setItemTime(webmType.getLongTime()+"月");
			vo.setOrderType(backOrder.getOrderType());
			return ResResult.build(200, "生成订单成功", vo);
		} catch (Exception e) {
			//返回错误代码
			e.printStackTrace();
			return ResResult.error(4, "生成订单失败");
		}
    }
    
    //我的订单列表
    public ResResult getMyOrderList(TokenData data){
    	try {
    		//判断token是不是存在
    		if(StringHelper.isEmpty(data.getToken())){
    			return ResResult.error(1, "token不能为空");
    		}
    		//先判断token正确性
    		boolean flag = TokenUtil.assertUserToken(data);
    		if(!flag){
    			return ResResult.error(2, "token失效");
    		}
    		
    		//从token中取account信息   
    		data.setAccount(TokenUtil.getUserList(data.getToken()).getAccount().trim());
    		//取得用户信息
    		WebMember member = webMemberService.getMemberByAccount(data.getAccount());
    		System.out.println("现在要查询的帐号是:"+member.getAccount());
    		
    		
    		List<MemberOrderVo> listVos = new ArrayList<MemberOrderVo>();
        	MemberOrderExample example = new MemberOrderExample();
        	example.createCriteria().andAccountEqualTo(member.getAccount());
    		List<MemberOrder> list = memberOrderService.list(example);
    		if(list!=null && list.size()>0){
    			for (MemberOrder backOrder : list) {
    				MemberOrderVo vo = new MemberOrderVo();
    				vo.setId(backOrder.getId());
    				vo.setAccount(backOrder.getAccount());
    				vo.setOrderNum(backOrder.getOrderNum());
    				vo.setRealname(member.getRealname());
    				vo.setItemCount(backOrder.getItemCount());
    				vo.setTotalPrice(backOrder.getTotalPrice());
    				vo.setNote(backOrder.getNote());
    				vo.setSource(backOrder.getSource());
    				vo.setStatus(backOrder.getStatus());
    				listVos.add(vo);
				}
    		}else{
    			listVos = new ArrayList<MemberOrderVo>();
    		}
    		
    		return ResResult.build(200, "我的订单查询成功", listVos);
    	} catch (Exception e) {
    		//返回错误代码
    		return ResResult.error(4, "我的订单查询失败");
    	}
    }


}